﻿using Siasun.AFC.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Siasun.AFC.RtPlatfromGui.IBll.Model
{
    /// <summary>
    /// 菜单信息实例
    /// </summary>
    public class MenuInfo
    {
        private string id;
        private string parentMenuId;
        private string menuName;
        private string dllName;
        private string dllFilePath;
        private string namespacePath;
        private string menuPageName;
        private int menuIsable;
        private int menuIsShow;
        private int menuIsShowDialog;
        private DateTime menuMedifyTime;
        private string nodeType;

        /// <summary>
        /// 菜单Id
        /// </summary>
        public string Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        /// <summary>
        /// 父节点Id
        /// </summary>
        public string ParentMenuId
        {
            get
            {
                return parentMenuId;
            }

            set
            {
                parentMenuId = value;
            }
        }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string MenuName
        {
            get
            {
                return menuName;
            }

            set
            {
                menuName = value;
            }
        }

        /// <summary>
        /// 动态库名称
        /// </summary>
        public string DllName
        {
            get
            {
                return dllName;
            }

            set
            {
                dllName = value;
            }
        }

        /// <summary>
        /// 动态库地址
        /// </summary>
        public string DllFilePath
        {
            get
            {
                return dllFilePath;
            }

            set
            {
                dllFilePath = value;
            }
        }

        /// <summary>
        /// 命名空间
        /// </summary>
        public string NamespacePath
        {
            get
            {
                return namespacePath;
            }

            set
            {
                namespacePath = value;
            }
        }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string MenuPageName
        {
            get
            {
                return menuPageName;
            }

            set
            {
                menuPageName = value;
            }
        }

        /// <summary>
        /// 菜单是否禁用
        /// </summary>
        public int MenuIsable
        {
            get
            {
                return menuIsable;
            }

            set
            {
                menuIsable = value;
            }
        }

        /// <summary>
        /// 菜单是否显示
        /// </summary>
        public int MenuIsShow
        {
            get
            {
                return menuIsShow;
            }

            set
            {
                menuIsShow = value;
            }
        }

        /// <summary>
        /// 菜单是否时模式显示
        /// </summary>
        public int MenuIsShowDialog
        {
            get
            {
                return menuIsShowDialog;
            }

            set
            {
                menuIsShowDialog = value;
            }
        }

        /// <summary>
        /// 菜单最后修改时间
        /// </summary>
        public DateTime MenuMedifyTime
        {
            get
            {
                return menuMedifyTime;
            }

            set
            {
                menuMedifyTime = value;
            }
        }

        /// <summary>
        /// 菜单节点
        /// </summary>
        public string NodeType
        {
            get
            {
                return nodeType;
            }

            set
            {
                nodeType = value;
            }
        }

        /// <summary>
        /// 实体类转换信息类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="modle"></param>
        /// <returns></returns>
        public static MenuInfo ConvertInfo<T>(T modle)
        {
            try
            {
                //原有数据
                PropertyInfo[] modelPro = modle.GetType().GetProperties();
                //返回值
                MenuInfo info = new MenuInfo();
                PropertyInfo[] changeBoxInfoPro = info.GetType().GetProperties();

                foreach (PropertyInfo itemInfo in changeBoxInfoPro)
                {
                    foreach (PropertyInfo itemModel in modelPro)
                    {
                        if (itemModel.Name.ToLower() == itemInfo.Name.ToLower() && itemModel.PropertyType == itemInfo.PropertyType)
                        {
                            object value = itemModel.GetValue(modle, null);
                            itemInfo.SetValue(info, value, null);
                        }
                    }
                }
                return info;
            }
            catch(Exception ex)
            {
                LoggerInf.Error("转换类型的时候出现错误，检查对象类型！");
                return new MenuInfo();
            }
        }

        /// <summary>
        /// 类型转换实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <returns></returns>
        public static T ConvertData<T>(MenuInfo info)
        {
            try
            {
                PropertyInfo[] infoPro = info.GetType().GetProperties();
                T model = Activator.CreateInstance<T>();
                Type type = model.GetType();
                PropertyInfo[] modelPro = model.GetType().GetProperties();

                foreach (PropertyInfo modelItem in modelPro)
                {
                    foreach (PropertyInfo infoItem in infoPro)
                    {
                        if (modelItem.Name.ToLower() == infoItem.Name.ToLower() && modelItem.PropertyType == infoItem.PropertyType)
                        {
                            object value = infoItem.GetValue(infoItem, null);
                            modelItem.SetValue(modelItem, value, null);
                        }
                    }
                }
                return model;
            }
            catch
            {
                Common.LoggerInf.Error("转换类型的时候出现错误，检查对象类型！");
                return default(T);
            }
        }
    }
}
